<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * [getRand 随机产生1-47的数字]
 * @param  integer $num [个数]
 * @return [type]       [数组]
 */
function getRand($num=7)
{
	$arr = [];

	for ($i=0; $i <$num ; $i++) {

		 $row = mt_rand(1,47);

		 if(in_array($row, $arr)){
		 	$i--;
		 }else{
		 	$arr[] = $row;
		 }
	}

	return $arr;
}

function getRand1($num=7)
{
	$arr = [];

	for ($i=0; $i <$num ; $i++) { 

		 $is = true;

		 $row = mt_rand(1,47);

		 foreach ($arr as $v) {
		 	if($v==$row){
		 		$is = false;
		 	}
		 }

		 if($is){
		 	$arr[] = $row;
		 }else{
		 	$i--;
		 }
		 
	}

	return $arr;
}

/**
 * [formatSize 格式化存储单位]
 * @param  [type] $size [字节数]
 * @return [type]       [可以理解的单位，kb mb gb tb]
 */
function formatSize($size)
{
	if($size<1024){
		return  $size.'b';
	}elseif($size<pow(1024,2)){
		return round($size/pow(1024,1),2)."kb";
	}elseif($size<pow(1024,3)){
		return round($size/pow(1024,2),2)."mb";
	}elseif($size<pow(1024,4)){
		return round($size/pow(1024,3),2)."gb";
	}else{
		return round($size/pow(1024,4),2)."tb";
	}
}
/**
 * [formatTime 格式化时间]
 * @param  [type] $time [时间戳]
 * @return [type]       [友好的时间格式]
 */
function formatTime($time)
{
	$diff = time()-$time;
	if($diff<60){
		return "刚刚";
	}elseif($diff<3600){
		return floor($diff/60)."分钟之前";
	}elseif($diff<86400){
		return floor($diff/3600)."小时之前";
	}elseif($diff<86400*7){
		return floor($diff/86400)."天之前";
	}else{
		return date("Y-m-d H:i:s",$time);
	}
}
/**
 * [getRandStr 随机产生指定个数的字符串]
 * @param  integer $num    [个数]
 * @param  boolean $is_num [是否纯数字，true 纯数字]
 * @return [type]          [随机的字符串]
 */
function getRandStr($num=16,$is_num=false)
{
	$str="qwertyuiopasdfghjkllzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";

	$str1 = "1234567890";

	$temp = "";

	for ($i=0; $i <$num ; $i++) { 
		if($is_num){
			$temp .=$str1[mt_rand(0,strlen($str1)-1)];
		}else{
			$temp .=$str[mt_rand(0,strlen($str)-1)];
		}
	}
	return $temp;
}
/**
 * [changeType 转手机格式]
 * @param  [type] $phone [原手机格式]
 * @return [type]        [转后的格式]
 */
function changeType($phone)
{
	$temp ="";

	for ($i=0; $i <strlen($phone) ; $i++) {

		$temp .= $phone[$i];

		if($i==2 || $i==6){
			$temp .= "-";
		}
	}
	return $temp;
}
/**
 * [changeHump 中划线转驼峰]
 * @param  [type] $str [字符串]
 * @return [type]      [转后的字符串]
 */
function changeHump($str)
{
	$arr = explode('-',$str);
	$temp ="";
	foreach ($arr as $k => $v) {
		if($k==0){
			$temp .= $v;
		}else{
			$temp .= ucfirst($v);
		}
	}
	return $temp;
}